import dayjs from "dayjs";
import { uniq, forEach, indexOf } from "lodash";

import { formatDateTimeSS } from "~/modules/shared/utils/common";
import { axisData } from "~/modules/worker/schedule";
import { Chart, ChartItem } from "~/modules/workspace/models";

// 图表数据偏移
export const chartShifting = (chartData: any, item: ChartItem, key: string) => {
  const data = chartData.map((t: { [propName: string]: any }) => {
    t["systime"] =
      t["systime"] &&
      formatDateTimeSS(
        dayjs(t["systime"]).valueOf() + 1000 * Number(item.XOffset) ||
          t["systime"]
      );

    axisData.offsetAxisData.push(t["systime"]);

    return [t["systime"], Number(t[key]) + Number(item.YOffset)];
  });
  axisData.offsetAxisData = uniq(axisData.offsetAxisData);
  return data;
};

// 获取状态量参数
export const getStateParamsOPtions = (data: ChartItem[]) => {
  let options: any[] = [];
  forEach(data, (item: ChartItem) => {
    forEach(item.data, (k) => {
      if (k.paramType === "Field") {
        options.push({ label: k.title, value: k.title });
      } else {
        options.push({ label: k.title, value: k.expression });
      }
    });
  });
  options = uniq(options);
  return options;
};
// 获取刨面图参数
export const getParamsOptions = (data: ChartItem[]) => {
  let options: any[] = [];
  if (data[0]?.IsTop) {
    forEach(data[0].data, (item: Chart) => {
      if (item.paramType === "Field") {
        options.push({ label: item.title, value: item.title });
      } else {
        options.push({ label: item.title, value: item.expression });
      }
    });
  } else {
    forEach(data, (item: ChartItem) => {
      forEach(item.data, (k) => {
        if (k.paramType === "Field") {
          options.push({ label: k.title, value: k.title });
        } else {
          options.push({ label: k.title, value: k.expression });
        }
      });
    });
  }
  options = uniq(options);
  return options;
};

// 状态量背景色设置
export const setStateBackgrounColor = (
  stateParamData: any[],
  state: boolean,
  stateParam: string
) => {
  const arrTime: any[][] = [];
  const paramDatas = stateParamData;
  const timeSlot: string[] = [];
  let timeData: string = "";
  const fileEndTimes: string[] = [];
  if (state && paramDatas && paramDatas.length > 0) {
    timeData = paramDatas[0][stateParam];
    timeSlot.push(paramDatas[0].systime);
    forEach(paramDatas, (item, index) => {
      if (item[stateParam] !== timeData) {
        timeData = item[stateParam];
        if (item.systime) {
          timeSlot.push(item.systime);
        } else {
          timeSlot.push(paramDatas[index - 1].systime);
          fileEndTimes.push(paramDatas[index - 1].systime);
        }
      }
    });
    timeSlot.push(paramDatas[paramDatas.length - 2].systime);

    forEach(timeSlot, (m, index) => {
      const arr2 = [];
      if (timeSlot[index + 1]) {
        arr2.push(dayjs(m).format("YYYY-MM-DD HH:mm:ss.SSS"));
        arr2.push(dayjs(timeSlot[index + 1]).format("YYYY-MM-DD HH:mm:ss.SSS"));
      }
      if (indexOf(fileEndTimes, arr2[0]) === -1) {
        arrTime.push(arr2);
      }
    });
  }
  return arrTime;
};
